package cn.luckday;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

@Slf4j
public class ServiceHandler implements Runnable {
    private final Socket client;

    public ServiceHandler(Socket client) {
        this.client = client;
    }

    @Override
    public void run() {
        try {

            log.info("from client: {}", client.getInetAddress());
            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintWriter out = new PrintWriter(client.getOutputStream(), true);
            String message = in.readLine();
            log.info("Received client data: {}", message);
            String returnMessage = "你好";
            log.info("Return client data: {}", returnMessage);
            client.close();
        } catch (Exception e) {
            log.error("Error closing client socket: {}", e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                log.error("Error closing client socket: {}", e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
